package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.vo.AlertDataHandleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author syl
 * @date 2025-06-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult handleAlertData(AlertDataHandleVo handleVo) {
        // 1. 参数校验
        if (handleVo.getId() == null) {
            return AjaxResult.error("报警数据ID不能为空");
        }

        // 2. 查询报警数据是否存在
        AlertData alertData = alertDataMapper.selectAlertDataById(handleVo.getId());
        if (alertData == null) {
            return AjaxResult.error("报警数据不存在");
        }

        // 3. 检查是否已处理
        if (alertData.getStatus() != null && alertData.getStatus() == 1) {
            return AjaxResult.error("该报警数据已处理，请勿重复操作");
        }

        // 4. 更新处理信息
        AlertData updateData = new AlertData();
        updateData.setId(handleVo.getId());
        updateData.setProcessingResult(handleVo.getProcessingResult());
        updateData.setProcessingTime(LocalDateTime.now());
        updateData.setProcessorId(SecurityUtils.getUserId());
        updateData.setProcessorName(SecurityUtils.getUsername());
        updateData.setStatus(1); // 设置为已处理状态

        // 5. 执行更新
        int result = alertDataMapper.updateAlertData(updateData);
        if (result > 0) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.error("操作失败");
        }
    }

}
